package ru.sushka.lunch.dao.files;

import java.text.ParseException;

import ru.sushka.lunch.enitiy.Course;

/**
 * 10.10.12 : 22:26 User: sushka формат csv:name;price;category;consist;countable
 * id - номер строки
 */
public class FileCourseDao extends FileDao<Course> {

    final static String FILE_NAME = "./db/course.csv";

    protected Course parseCsv(String csvline) throws ParseException {

        // validating
        if (csvline.trim().length() == 0) {
            throw new ParseException(csvline, 0);
        }
        String[] args = csvline.split(";");
        if (args.length != 5) {
            throw new ParseException(csvline, 0);
        }

        String name = args[0].trim();
        double price = Double.parseDouble(args[1].trim());
        int category = Integer.parseInt(args[2].trim());
        String consist = args[3].trim();
        boolean countable = Boolean.parseBoolean(args[4].trim());
        
        return new Course(name, price, category, consist, countable);

    }

    protected String convertToCsv(Course course) {
        String name = course.getName();
        String price = String.valueOf(course.getPrice());
        String category = String.valueOf(course.getCategory());
        String consist = course.getConsist();
        boolean countable = course.isCountable();
        return name + ";" + price + ";" + category + ";" + consist + ";" + String.valueOf(countable);
    }

    @Override
    protected String getFileName() {
        return FILE_NAME;
    }
}
